package cn.com.self.apis.portal.controller;


import cn.com.self.apis.core.vo.ResultModel;
import cn.com.self.apis.portal.entity.SysMenuEntity;
import cn.com.self.apis.portal.entity.SysRoleMenuEntity;
import cn.com.self.apis.portal.service.intf.IMenuService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.portal.vo.TreeNodesModel;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.Constants;
import cn.com.selfsoft.smartcity.commons.exception.EntityNotFoundException;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description
 */
@Api(tags = "菜单信息")
@Controller
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private IMenuService sysMenuService;



     /**分页获取已授权菜单
      * @Description //TODO
      * @return
      **/
     @ApiOperation(value = "分页获取已授权菜单")
     @RequestMapping(value = "/page",method = RequestMethod.GET)
     @ResponseBody
    public ResponseEntity rolePriviPage(HttpServletRequest request ,Page page){
         page.setCurrent(Long.parseLong(request.getParameter("page")));
         Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
         //分页获取符合条件的已授权用户数据
         ICustomPage<SysMenuEntity> dataList = this.sysMenuService.selectPage(queryParams, page);


         PageImpl<SysMenuEntity> pageData = new PageImpl<>(dataList.getRows(),
                 PageRequest.of(Integer.parseInt( String.valueOf(dataList.getCurrent())) - 1,
                         Integer.parseInt(String.valueOf(dataList.getSize()))), dataList.getTotal());
         DataPage<SysMenuEntity> dataPage = new DataPage<>(pageData);

         return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(dataPage));


    }


    /**分页获取未授权菜单
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "分页获取未授权菜单")
    @RequestMapping(value = "/ntprivipage",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity ntRolePriviPage(HttpServletRequest request ,Page page){
        page.setCurrent(Long.parseLong(request.getParameter("page")));
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort","access_token");
        //分页获取符合条件的已授权用户数据
        ICustomPage<SysMenuEntity> dataList = this.sysMenuService.ntRolePriviPage(queryParams, page);

        PageImpl<SysMenuEntity> pageData = new PageImpl<>(dataList.getRows(),
                PageRequest.of(Integer.parseInt( String.valueOf(dataList.getCurrent())) - 1,
                        Integer.parseInt(String.valueOf(dataList.getSize()))), dataList.getTotal());
        DataPage<SysMenuEntity> dataPage = new DataPage<>(pageData);

        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(dataPage));


    }

    /**进行角色菜单授权
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "进行角色菜单授权")
    @RequestMapping(value = "/granprivile",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity grantPrivile(HttpServletRequest request,@RequestBody List<SysRoleMenuEntity> list){
        sysMenuService.grantPrivile(list);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));

    }

    /**批量删除角色菜单授权信息
     * @Description //TODO
     * @return
     **/
    @ResponseBody
    @ApiOperation(value = "批量删除角色菜单授权信息")
    @RequestMapping(value = "/deletes",method = RequestMethod.POST)
    public ResponseEntity deletes( @RequestBody List<SysRoleMenuEntity> list){
        this.sysMenuService.deletes(list);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    /**
     * 新增菜单
     *
     */
    @ApiOperation(value = "新增菜单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity create(@RequestBody SysMenuEntity entity, HttpServletRequest request){

        //再加菜单表
        SysMenuEntity spe=this.sysMenuService.create(entity);

        SysRoleMenuEntity srpe =new SysRoleMenuEntity();
        srpe.setRoleId("1");
        long l=6;
        srpe.setMenuId(spe.getId());
//        //赋予默认角色
        int roleSysMenuFlog=this.sysMenuService.roleAdd(srpe);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(spe));
    }
    /**
     * 修改菜单
     */
    @ApiOperation(value = "修改菜单")
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody SysMenuEntity entity, HttpServletRequest request){
        SysMenuEntity sysMenuEntity=this.sysMenuService.load(id);
        if(sysMenuService==null){
            throw new EntityNotFoundException("主键已存在,提交有误");
        }
        CopyUtils.copyProperties(entity, sysMenuEntity);

        SysMenuEntity info =  this.sysMenuService.update(sysMenuEntity,id);

        return  ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(info));
    }
    /**
     * 删除菜单
     */
    @ApiOperation(value = "删除菜单")
    @RequestMapping(value = {"/deleteInfo/{id}"}, method = RequestMethod.DELETE)
    public ResponseEntity deleteInfo(@PathVariable("id") String id, HttpServletRequest request) {
        this.sysMenuService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }
    /**
     * 根据id获取数据
     */
    @ApiOperation(value = "根据id获取数据")
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadId(@PathVariable("id") String id, HttpServletRequest request) {
        SysMenuEntity SysMenuEntity=this.sysMenuService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(SysMenuEntity));
    }
    /**
     * 查询一级菜单目录
     */
    @ResponseBody
    @ApiOperation(value = "查询一级菜单目录")
    @RequestMapping(method = RequestMethod.GET, params = {Constants.DEFAULT_MARK_PARAMETER + "=tree"})
    public ResponseEntity page(HttpServletRequest request,Page pageable) {
/*         Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page","sort");
        //分页获取角色数据
       IPage<SysMenuEntity> dataList = this.sysMenuService.selectPage(queryParams,pageable);
        // IPage<SysRoleEntity> dataList = this.sysMenuService.selectPage(queryParams, pageable);
        //对数据进行转换
        PageImpl<SysMenuEntity> pageData = new PageImpl<>(dataList.getRows(),
                        PageRequest.of(Integer.parseInt( String.valueOf(dataList.getCurrent())) - 1,
                        Integer.parseInt(String.valueOf(dataList.getSize()))), dataList.getTotal());
        DataPage<SysMenuEntity> dataPage = new DataPage<>(pageData);*/
        List<SysMenuEntity> SysMenuIdList=sysMenuService.loadOneTree();

        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(SysMenuIdList));
    }
    /**
     * 查询二级菜单
     */

    /**
     * 登录时，查出该用户的菜单
     */
    @ApiOperation(value = "登录时，查出该用户的菜单")
    @RequestMapping(value = {"/sysMenu/load/{usersId}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadOneById(@PathVariable("usersId") String usersId, HttpServletRequest request) {
        List<Map<String,Object>> rtList=this.sysMenuService.loadOneSysMenu(usersId);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(rtList));
    }

    /**获取菜单树
     * @Description //TODO
     * @return
     **/
    @ApiOperation(value = "获取菜单树")
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity createMenuTree(HttpServletRequest request){
        Map<String, Object> queryMap = ParameterUtil.HumpToUnderline(request.getParameterMap(),"mark","sort","access_token");
        TreeNodesModel treeNodesModel = sysMenuService.createMenuTree(queryMap);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(treeNodesModel)));

    }


}
